class Solution {
public:
    using LL = long long;
    vector<long long> getDistances(vector<int>& arr) {
        vector<LL> res(arr.size(), 0);
        unordered_map<int, vector<int>> hash;
        for (int i = 0; i < arr.size(); i ++ )
            hash[arr[i]].push_back(i);
        for (auto it = hash.begin(); it != hash.end(); it ++ ) {
            vector<int> idxs = it->second;
            sort(idxs.begin(), idxs.end());
            LL sum = 0;
            for (auto & num : idxs) sum += num;
            LL pre = 0;
            for (int i = 0; i < idxs.size(); i ++ ) {
                pre += idxs[i];
                LL next = sum - pre;
                res[idxs[i]] = (LL)idxs[i] * (i + 1) - pre + next - (LL)idxs[i] * (idxs.size() - i - 1);
            }
        }
        return res;
    }
};
